package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
// import com.github.pagehelper.Page;
// import com.github.pagehelper.PageHelper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.annotation.OperateLogAnnotation;
import com.itheima.mapper.DeptMapper;
import com.itheima.mapper.EmpExprMapper;
import com.itheima.mapper.EmpMapper;
import com.itheima.pojo.Emp;
import com.itheima.pojo.EmpExpr;
import com.itheima.pojo.EmpQueryParam;
import com.itheima.pojo.PageBean;
import com.itheima.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Service
public class EmpServiceImpl extends ServiceImpl<EmpMapper,Emp> implements EmpService {
    @Autowired
    private EmpExprMapper empExprMapper;

    @Autowired
    private EmpMapper empMapper;

    @Autowired
    private DeptMapper deptMapper;


    // @Override
    // public PageBean getEmps(Integer page,Integer pageSize) {
    //     System.out.println("page = " + page);
    //     System.out.println("page = " + pageSize);
    //     Long listCount = empMapper.getListCount();
    //     Integer startIndex=(page-1)* pageSize;
    //     List<Emp> empsList = empMapper.getEmpsList(startIndex,pageSize);
    //     PageBean pageBean = new PageBean();
    //     pageBean.setTotal(listCount);
    //     pageBean.setRows(empsList);
    //     return pageBean;
    // }

    @Override
    @Transactional(propagation = Propagation.REQUIRED )
    public PageBean<Emp> getEmps(EmpQueryParam empQueryParam) {
        // 1，利用PageHelper设置 分页
        // PageHelper.startPage(empQueryParam.getPage(), empQueryParam.getPageSize());

        // 2,执行SQL 并把返回结果 强制转为Page类型，得到1个page 对象
        // Page pageHelperBean = (Page) empMapper.getEmpsList(empQueryParam);

        Page<Emp> page = new Page<>(empQueryParam.getPage(), empQueryParam.getPageSize());

        LambdaQueryWrapper<Emp> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(empQueryParam.getName()!=null , Emp::getName,empQueryParam.getName())
                .eq(empQueryParam.getGender()!=null , Emp::getGender,empQueryParam.getGender())
                .between(empQueryParam.getBegin()!=null && empQueryParam.getEnd()!=null ,
                        Emp::getEntryDate,empQueryParam.getBegin(),empQueryParam.getEnd());



        page = super.page(page,null);

        // page =  this.page(page,null);


        page.getRecords().forEach((item)->{
            item.setDeptName(deptMapper.selectById(item.getDeptId()).getName());
        });
        long total = page.getTotal();


        // 3,从page对象中获取 总数 和 当查询到的当前页的内容
        // long total = pageHelperBean.getTotal();
        // List<Emp> result = pageHelperBean.getResult();


        PageBean<Emp> pageBean = new PageBean();
        pageBean.setTotal(total);
        pageBean.setRows( page.getRecords());
        return pageBean;
    }
    // @Override
    // @Transactional(propagation = Propagation.REQUIRED )
    // public PageBean<Emp> getEmps(EmpQueryParam empQueryParam) {
    //     // 1，利用PageHelper设置 分页
    //     PageHelper.startPage(empQueryParam.getPage(), empQueryParam.getPageSize());
    //
    //     // 2,执行SQL 并把返回结果 强制转为Page类型，得到1个page 对象
    //     Page pageHelperBean = (Page) empMapper.getEmpsList(empQueryParam);
    //     // 3,从page对象中获取 总数 和 当查询到的当前页的内容
    //     long total = pageHelperBean.getTotal();
    //     List<Emp> result = pageHelperBean.getResult();
    //
    //
    //     PageBean<Emp> pageBean = new PageBean();
    //     pageBean.setTotal(total);
    //     pageBean.setRows(result);
    //     return pageBean;
    // }

    @Override
    @Transactional
    @OperateLogAnnotation
    public boolean save(Emp emp) {

        emp.setCreateTime(LocalDateTime.now());
        emp.setUpdateTime(LocalDateTime.now());
        emp.setPassword("123456");

        System.out.println("emp1 = " + emp);


        empMapper.insert(emp);


        // empMapper.save(emp);

        System.out.println("emp2 = " + emp);


        List<EmpExpr> exprList = emp.getExprList();

        if (!CollectionUtils.isEmpty(exprList)) {
            exprList.forEach((empExpr) -> {
                empExpr.setEmpId(emp.getId());
            });
              empExprMapper.saveBatch(exprList);
        }

      return true;

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    @OperateLogAnnotation
    public void delBatchEmp(List<Integer> ids) {

        //     1,删除员工，2删除员工经历
        // empMapper.delBatchEmp(ids);
        empMapper.deleteBatchIds(ids);

        //     删除员工经历
        empExprMapper.delBatchEmp(ids);

    }

    @Override
    public Emp selectEmpById(Integer id) {

        // Emp emp = empMapper.selectEmpById(id);
        Emp emp = empMapper.selectById(id);

        List<EmpExpr> empExprList= empExprMapper.selectEmpById(id);

        emp.setExprList(empExprList);

        return emp;
    }

    @Override
    @OperateLogAnnotation
    @Transactional(rollbackFor = Exception.class)
    public void updateEmpInfo(Emp emp) {

        emp.setUpdateTime(LocalDateTime.now());
        Integer id = emp.getId();
        List<EmpExpr> exprList = emp.getExprList();


        //   1，修改员工信息
        // empMapper.updateEmpInfo(emp);
        empMapper.updateById(emp);
    //     2，删除员工经历
        empExprMapper.delBatchEmp(Arrays.asList(id));
    //     3，新增员工信息
        if(!CollectionUtils.isEmpty(exprList)){
            exprList.forEach((expr)->{
                expr.setEmpId(id);
            });
            empExprMapper.saveBatch(exprList);

        }

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<Emp> getAllEmps() {
        // List<Emp>  list=  empMapper.getAllEmps();
        LambdaQueryWrapper<Emp> queryWrapper = new LambdaQueryWrapper<>();
        List<Emp>  list=  empMapper.selectList(queryWrapper);

        return list;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<Emp> selectEmpByDeptId(Integer id) {
        // List<Emp> list =  empMapper.selectEmpByDeptId(id);

        Emp emp =  empMapper.selectById(id);

        List<Emp> list = new ArrayList<>();

        list.add(emp);
        return list;
    }


}
